package com.example.login;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.example.login.fragment.CarFragment;
import com.example.login.fragment.HomeFragment;
import com.example.login.fragment.MineFragment;
import com.example.login.fragment.OrderFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private HomeFragment homeFragment;
    private OrderFragment orderFragment;
    private CarFragment carFragment;
    private MineFragment mineFragment;

    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        // 初始化控件
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                if (menuItem.getItemId() == R.id.home) {
                    selectedFragment(0);
                } else if (menuItem.getItemId() == R.id.car) {
                    selectedFragment(1);
                } else if (menuItem.getItemId() == R.id.order) {
                    selectedFragment(2);
                } else {
                    selectedFragment(3);
                }
                return true;
            }
        });

        // 默认首页选中
        selectedFragment(0);
    }

    private void selectedFragment(int position) {
//        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        androidx.fragment.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);

        if (position == 0) {
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
                fragmentTransaction.add(R.id.content, homeFragment);
            } else {
                fragmentTransaction.show(homeFragment);
            }
        } else if (position == 1){
            if (carFragment == null) {
                carFragment = new CarFragment();
                fragmentTransaction.add(R.id.content,carFragment);
            } else {
                fragmentTransaction.show(carFragment);
            }
        } else if (position == 2){
            if (orderFragment == null) {
                orderFragment = new OrderFragment();
                fragmentTransaction.add(R.id.content, orderFragment);
            } else {
                fragmentTransaction.show(orderFragment);
            }
        } else if (position == 3){
            if (mineFragment == null) {
                mineFragment = new MineFragment();
                fragmentTransaction.add(R.id.content, mineFragment);
            } else {
                fragmentTransaction.show(mineFragment);
            }
        }

        // 这里一定要提交
        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (homeFragment != null) {
            fragmentTransaction.hide(homeFragment);
        }
        if (orderFragment != null) {
            fragmentTransaction.hide(orderFragment);
        }
        if (carFragment != null) {
            fragmentTransaction.hide(carFragment);
        }
        if (mineFragment != null) {
            fragmentTransaction.hide(mineFragment);
        }
    }
}